#include"DemoPrismaticJoint.h"
#include"LSYEasyWorld.h"
#include"LSYEasyBody.h"
#include"BallMoveContactListener.h"
#include"BallMover.h"
#include"Tool.h"

USING_NS_CC;

bool DemoPrismaticJoint::init()
{
	if (!AbstractLayer::init(b2Vec2(0, -10)))
	{
		return false;
	}
	return true;
}

void DemoPrismaticJoint::box2DAppReady()
{
	createBodies();
	createJoint();
	
	listener = new BallMoveContactListener(this);
	pWorld->SetContactListener(listener);
}

void DemoPrismaticJoint::createJoint()
{
	b2Body* bodyA = LSYEasyBody::createCircle(b2Vec2(300, 250), 40, b2BodyType::b2_kinematicBody);
	bodyA->SetAngularVelocity(PI / 2);
	b2Body* bodyB = LSYEasyBody::createBox(b2Vec2(300, 110), b2Vec2(100, 10));
	
	b2Vec2 anchorA = b2Vec2(300 * PTM_RADIO, 210 * PTM_RADIO);
	b2Vec2 anchorB = b2Vec2(300 * PTM_RADIO, 110 * PTM_RADIO);
	b2DistanceJointDef* jointDef = new b2DistanceJointDef;
	jointDef->Initialize(bodyA, bodyB, anchorA, anchorB);
	pWorld->CreateJoint(jointDef);
	REALSE_NULL(jointDef);

	b2BodyDef* bd = new b2BodyDef;
	bd->position.Set(300 * PTM_RADIO, 50 * PTM_RADIO);
	bodyA = pWorld->CreateBody(bd);
	b2Vec2 anchor = bodyB->GetPosition();
	b2Vec2 verticalAxis = b2Vec2(0, 1);
	b2PrismaticJointDef* jointDef1 = new b2PrismaticJointDef;
	jointDef1->Initialize(bodyA, bodyB, anchor, verticalAxis);
	pWorld->CreateJoint(jointDef1);
	REALSE_NULL(jointDef1);
	/*b2BodyDef* bd = new b2BodyDef;
	bd->position = b2Vec2(400 * PTM_RADIO, 100 * PTM_RADIO);
	b2Body* bodyA = pWorld->CreateBody(bd);

	b2Body* car = LSYEasyBody::createBox(b2Vec2(200, 300), b2Vec2(100, 10));
	b2PolygonShape* shape = new b2PolygonShape;
	shape->SetAsBox(10 * PTM_RADIO, 30 * PTM_RADIO, b2Vec2(50 * PTM_RADIO, 10 * PTM_RADIO), 0);
	car->CreateFixture(shape, 1);
	shape->SetAsBox(10 * PTM_RADIO, 30 * PTM_RADIO, b2Vec2(-50 * PTM_RADIO, 10 * PTM_RADIO), 0);
	car->CreateFixture(shape, 1);
	
	b2Vec2 anchor(200 * PTM_RADIO, 100 * PTM_RADIO);
	b2Vec2 axis(1, 0);
	b2PrismaticJointDef* jointDef = new b2PrismaticJointDef;
	jointDef->Initialize(bodyA, car, anchor, axis);
	jointDef->enableMotor = false;
	jointDef->maxMotorForce = 20;
	jointDef->motorSpeed = 0;
	pWorld->CreateJoint(jointDef);
	
	REALSE_NULL(bd);
	REALSE_NULL(shape);*/
}

void DemoPrismaticJoint::createBodies()
{
	auto size = Director::getInstance()->getWinSize();
	LSYEasyBody::createRectangle(b2Vec2(0, 0), size.width, size.height);
	LSYEasyBody::createBox(b2Vec2(0, size.height*0.5), b2Vec2(10, size.height), b2BodyType::b2_kinematicBody);
	LSYEasyBody::createBox(b2Vec2(size.width, size.height*0.5), b2Vec2(10, size.height), b2BodyType::b2_kinematicBody);
	LSYEasyBody::createBox(b2Vec2(size.width*0.5, 0), b2Vec2(size.width, 0), b2BodyType::b2_kinematicBody);
	LSYEasyBody::createBox(b2Vec2(50,50), b2Vec2(100, 100), b2BodyType::b2_kinematicBody);
	LSYEasyBody::createBox(b2Vec2(size.width - 50, 50), b2Vec2(100, 100), b2BodyType::b2_kinematicBody);
	
	player = LSYEasyBody::createCircle(b2Vec2(50, 200), 20);
	ballMover = new BallMover(player);
}

void DemoPrismaticJoint::update(float dt)
{
	AbstractLayer::update(dt);
	
	//ballMover->isReadyJum
	ballMover->update();
}

void DemoPrismaticJoint::onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event * event)
{
	ballMover->onKeyDown((int)keyCode);
}

void DemoPrismaticJoint::onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event * event)
{
	ballMover->onKeyUp((int)keyCode);
}
